package com.myamap.tool;

import org.CrossApp.lib.CrossAppActivity;

import android.os.Bundle;
import android.util.SparseArray;

/*
 * 高德2D地图帮助类
 * 
 */
public class MyAmapViewHelper 
{
	private static SparseArray<MyAmapView> s_AmapViews = new SparseArray<MyAmapView>();

	private static int viewTag = 0;
	
	private static Bundle mBundle = null;
	
	//地图加载成功
	public native static void onAmapLoaded(int tag);
	//地图点击回调
	public native static void onAmapClicked(int tag, double lat, double lng);
	//地图marker拖拽回调
	public native static void onAmapMarkerDragStart(int tag, String markertag, double lat, double lng);
	public native static void onAmapMarkerDrag(int tag, String markertag, double lat, double lng);
	public native static void onAmapMarkerDragEnd(int tag, String markertag, double lat, double lng);
	//地图marker点击事件
	public native static void onAmapMarkerClick(int tag, String markertag);
	//地图marker的InfoWindow点击
	public native static void onAmapMarkerInfoWindowClick(int tag, String markertag);
	//地图右上角定位按钮
	public native static void onAmapLocationEnd(int tag, int code, double lat, double lng, String formatAddr, 
			String province, String city, String district, String street, String number, String poi, String adcode);
	
	public static void  initBundle(Bundle bundle)
	{
		mBundle = bundle;
	}
	
    public static int createAMap() 
    {
        final int index = viewTag;
        createAMap(index);
        return viewTag++;
    }
    
	public static void createAMap(final int index)
	{
        CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
            	MyAmapView mymapView = new MyAmapView(CrossAppActivity.getContext(), viewTag, mBundle);
            	s_AmapViews.put(index, mymapView);
            }
        });
    }
	
    public static void setVisible(final int index, final boolean visible) 
    {
    	CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
		    	MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.setVisible(visible);	
		    	}
            }
        });
    }
	
	public static void destroyAMap(final int index)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
            	MyAmapView myAmapView = s_AmapViews.get(index);
            	if (myAmapView != null)
            	{
            		s_AmapViews.remove(index);
            		myAmapView.destroy();
            	}
            }
        });
	}
	
	public static void setMapCenter(final int index, final double lat, final double lng)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.setMapCenter(lat, lng);
		    	}
            }
        });
	}
	
	public static void startLocation(final int index)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
            	MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.startLocation();
		    	}
            }
        });
	}
	
	public static void setMapZoomLevel(final int index, final float level)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.setMapZoomLevel(level);
		    	}
            }
        });
	}
	
	public static void addMarker(final int tag, final String marktag, final String img, final boolean dragable, final String title, final String content, final double lat, final double lng)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(tag);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.addMarker(marktag, img, dragable, title, content, lat, lng);
		    	}
            }
        });
	}
	
	public static void deleteMarker(final int tag, final String marktag)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(tag);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.deleteMarker(marktag);
		    	}
            }
        });
	}
	
	public static void clearMarkers(final int tag)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(tag);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.clearMarkers();
		    	}
            }
        });
	}
	
	public static void setAMapRect(final int index, final int x, final int y, final int width, final int height)
	{
		CrossAppActivity.getContext().runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
				MyAmapView myAmapView = s_AmapViews.get(index);
		    	if (myAmapView != null)
		    	{
		    		myAmapView.setAMapRect(x, y, width, height);
		    	}
            }
        });
	}
}
